#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

class Main {
	const int MAXN = 200002;
	int n, m, ans;
	vector<int> father, dist;

	void prepare()
	{
		ans = 0;
		for (int i = 0; i <= n; i++) {
			father[i] = i;
			dist[i] = 0;
		}
	}
	int find(int i)
	{
		if (i != father[i]) {
			int tmp = father[i];
			father[i] = find(tmp);
			dist[i] += dist[tmp];
		}
		return father[i];
	}

	void unite(int l, int r, int v)
	{
		int lf = find(l), rf = find(r);
		if (lf != rf) {
			father[lf] = rf;
			dist[lf] = v + dist[r] - dist[l];
		}
	}

	bool check(int l, int r, int v)
	{
		if (find(l) == find(r)) {
			if ((dist[l] - dist[r]) != v) {
				return false;
			}
		}
		return true;
	}

    public:
	Main() : father(MAXN, 0), dist(MAXN, 0)
	{
		ans = 0;
	}

	void run()
	{
		while (cin >> n >> m) {
			n++;
			prepare();
			for (int i = 1, l, r, v; i <= m; i++) {
				cin >> l >> r >> v;
				r++;
				if (!check(l, r, v)) {
					ans++;
				} else {
					unite(l, r, v);
				}
			}
                        cout << ans << endl;
		}
	}
};

int main()
{
	Main app;
	app.run();
	return 0;
}